<?php

namespace backend\controllers;

use Yii;
use backend\models\SysMessage;
use backend\models\SysMessageSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ForbiddenHttpException;

/**
 * SysMessageController implements the CRUD actions for SysMessage model.
 */
class SysMessageController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    
    public function beforeAction($action)
    {
    	//如果未登录，则直接跳转
    	if(Yii::$app->user->isGuest){
    		return $this->redirect(['/site/login']);
    	}
    
    
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	if(\Yii::$app->user->can('sys-message')){
    		return true;
    	}else{
    		throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
    	}
    }

    /**
     * Lists all SysMessage models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SysMessageSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single SysMessage model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new SysMessage model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new SysMessage();

        if ($model->load(Yii::$app->request->post())) {
        	if(!empty($model->send_time_1))$model->send_time = strtotime($model->send_time_1);
        	$model->create_time = time();
        	if($model->type == '1')$model->icon = 36518;
        	if($model->type == '2')$model->icon = 36519;
        	if($model->type == '3')$model->icon = 0;
        	if($model->save()){return $this->redirect(['view', 'id' => $model->id]);}           
        	if(!$model->save()){
            	return $this->render('create', [
                	'model' => $model,
            	]);
        	}
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing SysMessage model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
        	
        	if(!empty($model->send_time_1))$model->send_time = strtotime($model->send_time_1);
        	$model->create_time = time();
        	if($model->type == '1')$model->icon = 36518;
        	if($model->type == '2')$model->icon = 36519;
        	if($model->type == '3')$model->icon = 0;
        	if($model->save()){return $this->redirect(['view', 'id' => $model->id]);}
        	if(!$model->save()){
        		echo "<script type='text/javascript'>alert('发送时间需设置在1分钟后');</script>";
        	 	return $this->redirect(Yii::$app->request->referrer);}
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing SysMessage model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the SysMessage model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SysMessage the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SysMessage::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    public function actionSelectUser($id)
    {
    	
    }
}
